Repository navigation
fix(mpr): refuse inserting a UnitID that already has a row before touching its file - #2037
Draft
italo-kalil-cesar wants to merge 4 commits into
Draft
italo-kalil-cesar wants to merge 4 commits into
italo-kalil-cesar wants to merge 4 commits into
Conversation
…ching its file insertUnit wrote the unit file and only then inserted the row. For a UnitID that already had a row, the write replaced the existing unit's file, the INSERT then failed on the primary key, and the clean-up removed the file it believed it had just written: the row stayed and its file was gone. The UnitID is now checked first and refused with 'unit <id> already exists; nothing was written', and the clean-up after a failed INSERT leaves the file alone when a row for the unit exists (another writer got in between the check and the INSERT). A new UnitID, and a delete followed by an insert of the same ID (create or modify), insert as before. The tests fail without the change: the existing unit's file is gone and the error is the raw constraint error. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cites mendixlabs#2036. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…insert race never touches it The up-front check refuses a UnitID that already has a row, but two writers can pass it together. insertUnit wrote the file first, so the loser still replaced the winner's file before its INSERT failed. The row is now inserted first, in a transaction (BEGIN; INSERT; write the file; COMMIT): the primary key decides who owns the UnitID, the loser fails at the INSERT with nothing written, and the winner's transaction holds the write lock until its file is on disk. A file that cannot be written rolls the row back, and a crash leaves what the old order left, at worst an orphan file that no row points at. The test that calls the new step for an existing UnitID fails with the old order (the winner's file is gone). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Updates the entry for mendixlabs#2036. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
italo-kalil-cesar
marked this pull request as draft
December 3, 2026 13:04
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2036
What was wrong
Writer.insertUnit(modelsdk/mpr/writer_core.go, MPR v2) wrote the unit file first and inserted the row afterwards. For aUnitIDthat already had a row, the write replaced the existing unit's file, the INSERT then failed on the primary key, and the clean-up removed "the file we just wrote": the row stayed and its file was gone. The same happens when two writers create one UnitID.What changes
insertUnitchecks the UnitID first (refuseExistingUnit) and refuses it withunit <id> already exists; nothing was written, before the journal, the directory or the file are touched.removeFileOfFailedInsert) leaves the file alone when a row for the unit exists now, which is the case when another writer got in between the check and the INSERT.create or modifyof a document), insert as before.writer_insert_guard.go(the two helpers) and the test file. A CHANGELOG line under Unreleased.Tests
writer_insert_existing_unit_test.go: an existing UnitID is refused, and the unit's file bytes, its ContentsHash and the transaction id are unchanged; a new UnitID still inserts (file and row); the clean-up keeps the file of an existing row and removes an orphan that no row points at.Fix proven to be the cause: with
writer_core.gofrom main the first test fails (the file is gone: "cannot find the file specified", and the error is the rawUNIQUE constraint failed: Unit.UnitID); with the change all three pass.Run locally
Windows, no make:
go build ./modelsdk/...,go vet ./modelsdk/mpr,gofmt -l modelsdk/mprclean.go test ./modelsdk/mpr -count=1: the only failures are the 12 that fail the same way on a clean main on Windows (TestCacheUnitReads_*,TestUnitStore_*,TestSyncWithDisk_Issue2121; #2016 fixes them).go test ./mdl/backend/modelsdk -run 'Recreate|Insert|Unit|Writer|Connect|SyncWithDisk': ok. With the built binary on a copy oftestdata/pedapp:execof a script that creates a module, entity, enumeration, microflow, page and REST client, then a script that re-creates the same documents (create or modify,create or replace) twice: exit 0 each time, the second run reports the documents already in sync, andmxcli fix hashesreports 0 mismatches, 0 missing files, 0 orphan files. Not run:make build,make lint-go,make check-findings(no make here), and the wholemdl/executorandcmd/mxclisuites (they do not finish on this machine); CI is the real check.Studio Pro validation
Not applicable: no model output changes for a script that did not hit this error.
Agentic testing
Not run.
Not covered
Update: the insert race is closed, and one more thing found next to it
The PR now has two more commits. The "Not covered" item about two writers reaching the INSERT together is no longer open:
writeNewUnitV2inwriter_insert_guard.go). The primary key decides who owns the UnitID, the loser fails at the INSERT with nothing written, and the winner's transaction holds SQLite's write lock until its file is on disk. A file that cannot be written rolls the row back. A crash leaves what the old order left: at worst an orphan file that no row points at (an insert of the same UnitID replaces it); there is never a row whose file was not written. The up-front check stays, for the clear message. Tests:TestWriteNewUnitV2_TheLoserOfTheInsertNeverTouchesTheFilefails with the old order (the winner's file is gone) and passes now;TestWriteNewUnitV2_AFileThatCannotBeWrittenLeavesNoRow.WriteTransaction.WriteUnitrefuses a unit that has no row (requireUnitRow, MPR v2 and v1). I looked at theWriteTransactionpath for the same pattern: it does not overwrite before an INSERT (it only UPDATEs the row of the unit it is given and renames a temp file at commit), but for a unit with no row the UPDATE matched nothing, silently, and the file was still renamed into place at commit: a file no row points at, reported as written. The only callers (codec.Store.SaveUnitandFlushUnits) write units they loaded, so it now returns an error and the caller rolls back. Tests:TestWriteUnit_AUnitWithNoRowIsRefusedAndNoFileIsLeftBehindandTestWriteUnit_AUnitWithARowStillWrites.One thing about the history: the WriteUnit change went into the same commit as the insert reordering (they share
writer_core.goand the test file), so that commit's message only mentions the insert. I can split it if you want it split.Re-run after the change, on Windows:
go test ./modelsdk/mpr -count=1: the same 12 failures as on main, none new;go test ./modelsdk/codec -count=1: ok;go test ./mdl/backend/modelsdk -run 'Recreate|Insert|Unit|Writer|Connect|SyncWithDisk|Move': ok; the built binary on a copy oftestdata/pedapp: create, then re-create twice (create or modify,create or replace): exit 0, the second run "already in sync",fix hashes: 0 mismatches, 0 missing files, 0 orphan files.